package wci.frontend.pascal.parsers;

import static wci.frontend.pascal.PascalTokenType.SEMICOLON;

import java.util.EnumSet;

import wci.frontend.Token;
import wci.frontend.pascal.PascalParserTD;
import wci.frontend.pascal.PascalTokenType;
import wci.intermediate.TypeSpec;

/**
 * <h1>TypeSpecificationParser</h1>
 *
 * <p>解析一个类型说明（规范或标准或其它）</p>
 *
 * <p>Copyright (c) 2009 by Ronald Mak</p>
 * <p>For instructional purposes only.  No warranties.</p>
 */
class TypeSpecificationParser extends PascalParserTD
{
    protected TypeSpecificationParser(PascalParserTD parent)
    {
        super(parent);
    }

    //可开始三种类型标准：简单、数组或记录
    static final EnumSet<PascalTokenType> TYPE_START_SET =
        SimpleTypeParser.SIMPLE_TYPE_START_SET.clone();
    static {
        TYPE_START_SET.add(PascalTokenType.ARRAY);
        TYPE_START_SET.add(PascalTokenType.RECORD);
        TYPE_START_SET.add(SEMICOLON);
    }

    /**
     * 解析一个类型说明
     * @param token 当前token
     * @return 解析到的类型说明
     * @throws Exception
     */
    public TypeSpec parse(Token token)
        throws Exception
    {
        token = synchronize(TYPE_START_SET);

        switch ((PascalTokenType) token.getType()) {
        	//数组必须是以 'array' 开头，array是关键字
            case ARRAY: {
                ArrayTypeParser arrayTypeParser = new ArrayTypeParser(this);
                return arrayTypeParser.parse(token);
            }
            //记录类型必须'record'开头，record是关键字
            case RECORD: {
                RecordTypeParser recordTypeParser = new RecordTypeParser(this);
                return recordTypeParser.parse(token);
            }
            
            default: {
                SimpleTypeParser simpleTypeParser = new SimpleTypeParser(this);
                return simpleTypeParser.parse(token);
            }
        }
    }
}
